package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.SangerCwfxyqMapper;
import com.zhijian.medical.entity.po.ExperimentProcessSampleLog;
import com.zhijian.medical.entity.po.Sanger;
import com.zhijian.medical.entity.po.SangerCwfxyq;
import com.zhijian.medical.entity.vo.request.ExperimentProcessSampleSearchVo;
import com.zhijian.medical.entity.vo.request.SangerCwfxyqSearchVo;
import com.zhijian.medical.entity.vo.response.ExperimentProcessSampleVo;
import com.zhijian.medical.entity.vo.response.SangerCwfxyqVo;
import com.zhijian.medical.enums.AllExperimentationStepStatusEnum;
import com.zhijian.medical.enums.ExperimentTypeEnum;
import com.zhijian.medical.enums.SangerStatusEnum;
import com.zhijian.medical.enums.StatusEnum;
import com.zhijian.medical.holder.CurrentUserHolder;
import com.zhijian.medical.service.ExperimentProcessSampleLogService;
import com.zhijian.medical.service.ExperimentProcessSampleService;
import com.zhijian.medical.service.SangerCwfxyqService;
import com.zhijian.medical.service.SangerService;
import com.zhijian.medical.util.AssertUtil;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 一代测序-产物分析一区 业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-08-12 14:29:43
 */
@Slf4j
@Service
public class SangerCwfxyqServiceImpl implements SangerCwfxyqService {

    @Autowired
    private SangerCwfxyqMapper mapper;

    @Autowired
    private SangerService sangerService;

    @Autowired
    private ExperimentProcessSampleService experimentProcessSampleService;

    @Autowired
    private ExperimentProcessSampleLogService experimentProcessSampleLogService;

    @Override
    public SangerCwfxyq selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public SangerCwfxyq selectBySangerId(Long sangerId) {
        return mapper.selectBySangerId(sangerId);
    }

    @Override
    public SangerCwfxyqVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public SangerCwfxyqVo selectVoBySangerId(Long sangerId) {
        SangerCwfxyqVo sangerCwfxyqVo = mapper.selectVoBySangerId(sangerId);
        if (Objects.isNull(sangerCwfxyqVo)) {
            return null;
        }
        Sanger sanger = sangerService.selectById(sangerId);
        if (Objects.isNull(sanger)) {
            return null;
        }
        sangerCwfxyqVo.setResult(sanger.getStatus() > SangerStatusEnum.CWFXYQ.getCode().byteValue() ?
                AllExperimentationStepStatusEnum.END.getCode().byteValue() : AllExperimentationStepStatusEnum.UN_END.getCode().byteValue());
        sangerCwfxyqVo.setResultStr(sanger.getStatus() > SangerStatusEnum.CWFXYQ.getCode().byteValue() ?
                AllExperimentationStepStatusEnum.END.getDesc() : AllExperimentationStepStatusEnum.UN_END.getDesc());
        return sangerCwfxyqVo;
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(SangerCwfxyq sangerCwfxyq) {
        Date now = new Date();
        sangerCwfxyq.setCreateTime(now);
        sangerCwfxyq.setUpdateTime(now);
        sangerCwfxyq.setAzfzzsop010pz(StatusEnum.UN_NORMAL.getCode().byteValue());
        sangerCwfxyq.setGcywyc(StatusEnum.UN_NORMAL.getCode().byteValue());
        sangerCwfxyq.setKbdzsfyc(StatusEnum.UN_NORMAL.getCode().byteValue());
        sangerCwfxyq.setMdtdsffhyq(StatusEnum.UN_NORMAL.getCode().byteValue());
        sangerCwfxyq.setClsyfqw(StatusEnum.UN_NORMAL.getCode().byteValue());
        sangerCwfxyq.setJxsyqzwxzs(StatusEnum.UN_NORMAL.getCode().byteValue());
        sangerCwfxyq.setJdyjgkbzppt(StatusEnum.UN_NORMAL.getCode().byteValue());
        sangerCwfxyq.setJpptgd(StatusEnum.UN_NORMAL.getCode().byteValue());
        sangerCwfxyq.setDztp(StatusEnum.NORMAL.getCode().byteValue());
        sangerCwfxyq.setWbl(StatusEnum.NORMAL.getCode().byteValue());
        sangerCwfxyq.setDyy(StatusEnum.NORMAL.getCode().byteValue());
        sangerCwfxyq.setNjcxy(StatusEnum.NORMAL.getCode().byteValue());
        return mapper.insert(sangerCwfxyq) > 0 ? sangerCwfxyq.getId() : 0L;
    }

    @Override
    public boolean update(SangerCwfxyq sangerCwfxyq) {
        Date now = new Date();
        sangerCwfxyq.setUpdateTime(now);
        return mapper.updateByPrimaryKey(sangerCwfxyq) != 0;
    }

    @Transactional(rollbackFor = Exception.class, value = "localTransactionManager")
    @Override
    public boolean saveOrUpdate(SangerCwfxyq sangerCwfxyq) {
        Long sangerId = sangerCwfxyq.getSangerId();
        Sanger sanger = sangerService.selectById(sangerId);
        AssertUtil.notNull(sanger, "此实验不存在或者已被删除");
        if (sanger.getStatus() < SangerStatusEnum.HSCHYQ1.getCode().byteValue()) {
            //将实验的状态修改
            sangerService.updateByPrimaryKeySelective(Sanger.builder()
                    .id(sangerId)
                    .status(SangerStatusEnum.HSCHYQ1.getCode().byteValue())
                    .build());
        }
        SangerCwfxyq oldSangerCwfxyq = this.selectBySangerId(sangerId);
        if (Objects.isNull(oldSangerCwfxyq)) {
            this.insert(sangerCwfxyq);
        } else {
            sangerCwfxyq.setId(oldSangerCwfxyq.getId());
            this.update(sangerCwfxyq);
        }
        List<ExperimentProcessSampleVo> experimentProcessSampleVos = experimentProcessSampleService.selectList(ExperimentProcessSampleSearchVo.builder()
                .mainType(ExperimentTypeEnum.SANGER.getCode().byteValue())
                .experimentProcessId(sangerId)
                .build());
        //保存样本日志
        experimentProcessSampleLogService.saveBatch(experimentProcessSampleVos.stream().map(e -> ExperimentProcessSampleLog.builder()
                .sampleId(e.getId())
                .fishId(sangerId)
                .mainType(ExperimentTypeEnum.SANGER.getCode().byteValue())
                .op(String.format("%s%s-%s", sanger.getName(), sanger.getProcessNumber(), SangerStatusEnum.CWFXYQ.getDesc()))
                .opUser(CurrentUserHolder.getUser().getName())
                .build()).collect(Collectors.toList()));
        return true;
    }

    @Override
    public Integer count(SangerCwfxyqSearchVo sangerCwfxyqSearchVo) {
        return mapper.count(sangerCwfxyqSearchVo);
    }

    @Override
    public List<SangerCwfxyqVo> selectList(SangerCwfxyqSearchVo sangerCwfxyqSearchVo) {
        return mapper.selectList(sangerCwfxyqSearchVo);
    }

    @Override
    public Page<SangerCwfxyqVo> selectPage(SangerCwfxyqSearchVo sangerCwfxyqSearchVo) {
        //先直接查总数
        int count = count(sangerCwfxyqSearchVo);
        //分页
        Page<SangerCwfxyqVo> page = new Page<>(true, count, sangerCwfxyqSearchVo.getPageNum(), sangerCwfxyqSearchVo.getPageSize());
        sangerCwfxyqSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<SangerCwfxyqVo> responseVos = this.selectList(sangerCwfxyqSearchVo);
        page.setData(responseVos);
        return page;
    }

}